package com.candy.test;

import com.candy.bean.UserInfo;
import com.candy.dao.UserInfoMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class DBDriver {
    @Test
    public  void test06() {
//        指定配置文件的目录
        String resource = "mybatis-config.xml";
        try {
//        读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            通过配置文件创建SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           获取SqlSession,SqlSession相当于JDBC中的statement类，用来执行增删改查方法
            try (SqlSession session = sqlSessionFactory.openSession()) {
                UserInfoMapper mapper=session.getMapper(UserInfoMapper.class);

                int num=  mapper.deleleUserInfoById(40);
                System.out.println(num);
//                增删改需要手动提交事务
                session.commit();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public  void test05() {
//        指定配置文件的目录
        String resource = "mybatis-config.xml";
        try {
//        读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            通过配置文件创建SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           获取SqlSession,SqlSession相当于JDBC中的statement类，用来执行增删改查方法
            try (SqlSession session = sqlSessionFactory.openSession()) {
                UserInfoMapper mapper=session.getMapper(UserInfoMapper.class);
                UserInfo userInfo=new UserInfo();
                userInfo.setUsername("黑桃7");
                userInfo.setId(44);
//                执行新增操作
                int num=  mapper.updateUserInfo(userInfo);
                System.out.println(num);
//                增删改需要手动提交事务
                session.commit();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public  void test04() {
//        指定配置文件的目录
        String resource = "mybatis-config.xml";
        try {
//        读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            通过配置文件创建SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           获取SqlSession,SqlSession相当于JDBC中的statement类，用来执行增删改查方法
            try (SqlSession session = sqlSessionFactory.openSession()) {
                UserInfoMapper mapper=session.getMapper(UserInfoMapper.class);
                UserInfo userInfo=new UserInfo();
                userInfo.setUsername("黑桃3");
                userInfo.setUserpwd("123456");
                userInfo.setUseradress("北京");
                userInfo.setUserTel("15698745698");
//                执行新增操作
                int num=  mapper.addUserInfo(userInfo);
                System.out.println(num);
//                增删改需要手动提交事务
                session.commit();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public  void test03() {
//        指定配置文件的目录
        String resource = "mybatis-config.xml";
        try {
//        读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            通过配置文件创建SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           获取SqlSession,SqlSession相当于JDBC中的statement类，用来执行增删改查方法
            try (SqlSession session = sqlSessionFactory.openSession()) {
                UserInfoMapper mapper=session.getMapper(UserInfoMapper.class);
                List<UserInfo> userInfoes=  mapper.selectAllUserInfoes();
                for (UserInfo userinfo:userInfoes
                     ) {
                    System.out.println(userinfo);
                }

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public  void test02() {
//        指定配置文件的目录
        String resource = "mybatis-config.xml";
        try {
//        读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            通过配置文件创建SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           获取SqlSession,SqlSession相当于JDBC中的statement类，用来执行增删改查方法
            try (SqlSession session = sqlSessionFactory.openSession()) {
                UserInfoMapper mapper=session.getMapper(UserInfoMapper.class);
                UserInfo userInfo=  mapper.selectUserInfoById(1);
                System.out.println(userInfo);

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public  void test01() {
//        指定配置文件的目录
        String resource = "mybatis-config.xml";
        try {
//        读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            通过配置文件创建SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           获取SqlSession,SqlSession相当于JDBC中的statement类，用来执行增删改查方法
            try (SqlSession session = sqlSessionFactory.openSession()) {

//                调用selectOne获取查询结果  第一个参数：是由mapper.xml文件中的namespace和id所组成，第二个参数：你需要传递进去的参数值
              UserInfo userInfo= session.selectOne("com.candy.dao.UserInfoMapper.selectUserInfoById",2);
//             打印查询结果
                System.out.println(userInfo);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
